<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <script src="layer/layer.js"></script>
    <style>
        .code {
            padding: 5px;
            width: 390px;
        }

        .code td {
            padding: 10px !important;
        }

        .code img {
            cursor: pointer
        }
        *{overflow: hidden}
    </style>
</head>
<body>
<?php
$id = $_GET['id'] ?? '';
if (!is_numeric($id) || $id == '') {
    echo "<script>alert('参数错误');history.back();</script>";
    exit;
}
?>
<div class="code">
    <table style="border-collapse: collapse" border="1" bordercolor="gray" cellspacing="0">
        <tr>
            <td align="right">验证码</td>
            <td align="left"><input name="code" id="code"><img src="code.php" onclick="this.src='code.php?'+new Date()" >
            </td>
        </tr>
        <tr>
            <td align="right"><input type="button" onclick="postVote(<?php echo $id; ?>);" value="提交"></td>
            <td align="left"><input type="reset" value="重置"></td>
        </tr>
    </table>
</div>
<script>
    function postVote(id) {
        let code = $("#code").val().trim();
        if(code == ''){
            layer.msg('验证码必须填写！');
            return;
        }
        $.ajax({
            url:'ajaxVote.php',
            data:{
                id:id,
                code:code
            },
            type:'get',
            dataType:'json',
            success:function (d){
                if(d.error == 1){
                    //说明投票出错或者失败
                    layer.msg(d.errMsg);
                }
                else{
                    //说明投票成功，更新当前车辆票数
                    window.parent.updateNum(id);
                    layer.msg(d.errMsg);
                    setTimeout(()=>{
                        window.parent.closeLayer();
                    },1500)
                }
            },
            error:function (){
                layer.msg('网络错误');
                window.parent.closeLayer();
            }
        })
    }
</script>
</body>
</html>
